home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: pointer questions
- Date: Wed, 20 Mar 1996 11:04:17 -0500
- Organization: Datalytics, Inc
- Message-ID: <31502C81.1AB6@datalytics.com>
- References: <4ifatf$5a8u@uvaix3e1.comp.UVic.CA> <4injoe$qcp@druid.borland.com>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Pete Becker wrote:
- >
- > In article <4ifatf$5a8u@uvaix3e1.comp.UVic.CA>, cgesy@uvaix.uvic.ca says...
- > >
- > >What does the following statement do/mean? :
- > >
- > > ((searchItem&) *this)._refCount++; //searchItem is the class name
- >
- > It tells the compiler to pretend that the 'this' pointer actually points to an
- > object of type searchItem, and to increment the member of that object named
- > _refCount.
-
- More to the point, dereferencing this (*this in the example)
- results in an object of whatever type this points to. That
- object is then cast to be a reference to class searchItem. This
- cast is only permissible if this points to a class for which a
- conversion to searchItem* is available (ARM 5.4). Note that
- constructors and conversion functions are not called as part of
- this conversion.
-
- The advantage of this cast to reference is that the result is an
- lvalue. That advantage is not put to use in your example,
- however.
-
- The example you gave would be better rendered thusly:
-
- (searchItem*(this))->refCount++;
-
- This form reduces the clutter and thus more clearly indicates
- that you want this to point to its searchItem part and increment
- the refCount data member. However, if searchItem is a base
- class of the class to which this points, and refCount is
- (obviously) a protected or public member of searchItem (suspect
- in itself, mind you), then you could just code this:
-
- refCount++;
-
- The last form, I'm sure you'll agree is far more readable.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-